package models

import (
	"io"
	"mime/multipart"
	"os"
	"path"
	"strings"
)

func GetImageUrlByImageFile(file multipart.File, m *multipart.FileHeader, err error) string {
	if err != nil {
		return ""
	}
	if m.Size > 2<<20 {
		return ""
	}
	PathStr := ".jpg.jpeg.png"
	if !strings.Contains(PathStr, path.Ext(m.Filename)) {
		return ""
	}
	openFile, err := os.OpenFile("./static/img/"+m.Filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 777)
	if err != nil {
		return ""
	}
	io.Copy(openFile, file)
	return "http://127.0.0.1:8080/static/img" + m.Filename
}
